//
//  ObjectUtils.swift
//  LHSystem
//
//  Created by YangZhe on 16/2/27.
//  Copyright © 2016年 YangZhe. All rights reserved.
//

public struct YSerializableUtils{
    public static func saveObj(obj:NSCoding,key:String){
        NSKeyedArchiver.archiveRootObject(obj, toFile:"\(NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true).first!)/\(key).archive")
    }
    
    public static func readObj(key:String) -> NSCoding?{
        return NSKeyedUnarchiver.unarchiveObject(withFile: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true).first!)/\(key).archive") as? NSCoding
    }
    
    public static func deleteObj(key:String){
        try? FileManager.default.removeItem(atPath: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask, true).first!)/\(key).archive")
    }
    
    public static func saveObjToCache(obj:NSCoding,key:String){
        NSKeyedArchiver.archiveRootObject(obj, toFile:"\(NSSearchPathForDirectoriesInDomains(.cachesDirectory,.userDomainMask, true).first!)/\(key).archive")
    }
    
    public static func readObjFromCache(key:String) -> NSCoding?{
        return NSKeyedUnarchiver.unarchiveObject(withFile: "\(NSSearchPathForDirectoriesInDomains(.cachesDirectory,.userDomainMask, true).first!)/\(key).archive") as? NSCoding
    }
}
